//
//  AYAreaTableViewController.m
//  QuickCure
//
//  Created by 张聪 on 16/5/5.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYAreaTableViewController.h"
#import "AYMAreaModel.h"
#import "AYMCityTableViewController.h"

@interface AYAreaTableViewController ()

/// 模型数组
@property (nonatomic, strong) NSArray *provinces;

@end

@implementation AYAreaTableViewController

static NSString *const ID = @"reuseIdentifier";

#pragma mark - 懒加载
- (NSArray *)provinces {
    
    if (_provinces == nil) {
        
        //读取路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        
        NSArray *pathArray = [NSArray arrayWithContentsOfFile:path];
        
        //字典转模型
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in pathArray) {
            
            AYMAreaModel *areaModel = [AYMAreaModel provinceWithDict:dict];
            
            [tempArray addObject:areaModel];
            
        }
        _provinces = tempArray.copy;        
    }
    return _provinces;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
 
}

#pragma mark - Table view data source

//设置组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

//设置行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.provinces.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    //获取模型数据
    AYMAreaModel *areaModel = self.provinces[indexPath.row];
    cell.textLabel.text = areaModel.name;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

//选中某一行跳转到城市控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    AYMCityTableViewController *cityViewController = [[AYMCityTableViewController alloc] init];
   
    //设置城市tableview的数据
    cityViewController.citys = [self.provinces[indexPath.row] cities];
    
    [self.navigationController pushViewController:cityViewController animated:YES];
    
}

@end
